from django.core.management.base import BaseCommand
from user.models import User, UserProfile


class Command(BaseCommand):
    help = '检查用户资料数据'

    def handle(self, *args, **options):
        # 检查用户总数
        user_count = User.objects.count()
        self.stdout.write(f"用户总数: {user_count}")
        
        # 检查用户资料总数
        profile_count = UserProfile.objects.count()
        self.stdout.write(f"用户资料总数: {profile_count}")
        
        # 显示所有用户
        users = User.objects.all()
        self.stdout.write("\n所有用户:")
        for user in users:
            self.stdout.write(f"  - ID: {user.id}, 用户名: {user.username}, 手机: {user.phone}")
        
        # 显示所有用户资料
        profiles = UserProfile.objects.select_related('user').all()
        self.stdout.write("\n所有用户资料:")
        for profile in profiles:
            self.stdout.write(f"  - 用户ID: {profile.user.id}, 昵称: {profile.nickname}, 性别: {profile.gender}")
        
        # 检查没有用户资料的用户
        users_without_profile = User.objects.filter(userprofile__isnull=True)
        self.stdout.write(f"\n没有用户资料的用户数量: {users_without_profile.count()}")
        
        if users_without_profile.exists():
            self.stdout.write("这些用户没有用户资料:")
            for user in users_without_profile:
                self.stdout.write(f"  - ID: {user.id}, 用户名: {user.username}")

